package com.chenyue.cm.account.controller;


import com.chenyue.cm.account.domain.JdAccountRecord;
import com.chenyue.cm.account.domain.JdWithdrawApply;
import com.chenyue.cm.account.domain.JdWithdrawApplyExample;
import com.chenyue.cm.account.service.JdAccountRecordService;
import com.chenyue.cm.account.service.JdWithdrawApplyService;
import com.chenyue.cm.base.aop.Log;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.RequestUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;

/**
 * @author chenyue-cm
 * @date 2021-07-20
 */
@RestController
@RequestMapping("/jdWithdrawApply")
public class JdWithdrawApplyController {

    private final JdWithdrawApplyService withdrawApplyService;

    private final JdAccountRecordService accountRecordService;

    public JdWithdrawApplyController(JdWithdrawApplyService jdWithdrawApplyService, JdAccountRecordService accountRecordService) {
        this.withdrawApplyService = jdWithdrawApplyService;
        this.accountRecordService = accountRecordService;
    }

    @RequestMapping("/getList")
    public Success getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        PageVo<JdWithdrawApply> result = withdrawApplyService.getPageBean(pageInfo, JdWithdrawApply.class);
        return Success.success(result);
    }

    /**
     * @param vo 插入实体
     * @return Success
     */
    @Log
    @RequestMapping("/save")
    public Success save(JdWithdrawApply vo) {
        boolean success = withdrawApplyService.save(vo);
        return success ? Success.success() : Success.error();
    }

    /**
     * @param vo      修改实体
     * @param request id
     * @return Success
     */
    @Log
    @RequestMapping("/edit")
    public Success edit(JdWithdrawApply vo, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            int success = withdrawApplyService.getBaseMapper().updateById(vo);
            return success > 0 ? Success.success() : Success.error();
        } else {
            return Success.error("id不能为空");
        }
    }

    /**
     * @param id 主键
     * @return Success
     */
    @Log
    @RequestMapping("/delete/{id}")
    public Success deleteByPrimaryKey(@PathVariable("id") Integer id) {
        JdWithdrawApplyExample aExample = new JdWithdrawApplyExample();
        aExample.createCriteria().andIdEqualTo(id);
        int success = withdrawApplyService.deleteByExample(aExample);
        return success > 0 ? Success.success() : Success.error();
    }

    /**
     * 【API】----  删除多个数据
     *
     * @return Success
     */
    @Log
    @RequestMapping("/delete")
    public Success delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            int success = withdrawApplyService.deleteByPrimaryKey(idString);
            if (success == 0) {
                return Success.error();
            }
        }
        return Success.success();
    }

    /**
     * 银行卡提现审核
     *
     * @param id     id
     * @param state  状态1通过2拒绝
     * @param remark 拒绝理由
     * @return Success
     */
    @Log
    @RequestMapping("/withdrawCheck")
    @Transactional(rollbackFor = Exception.class)
    public Success<String> withdrawCheck(
            Integer id, Integer state, String remark
    ) {
        final JdWithdrawApply one = withdrawApplyService.lambdaQuery().eq(JdWithdrawApply::getId, id).one();
        one.setApplyState(state);
        one.setRemark(remark);
        one.setEt(Calendar.getInstance().getTime());
        final boolean flag = withdrawApplyService.updateById(one);
        if (!flag) {
            return Success.error();
        }
        if (state == 2) {
//            审核拒绝退钱
            final JdAccountRecord accountRecord = accountRecordService.lambdaQuery().eq(JdAccountRecord::getOrderId, one.getId()).one();
            if (accountRecord != null) {
                accountRecordService.delete(accountRecord.getId());
            }
        }
        return Success.success();
    }
}
